var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var autoprefixer = require('autoprefixer');
var utils = require('./utils');

module.exports = {
    entry: {
        main: './src/main.js',
        vendor: ['vue', 'vue-router', 'mint-ui', 'fastclick', 'axios', 'chart.js']
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js'
    },
    module: {
        rules: [{
            test: /\.vue$/,
            loader: 'vue-loader'
        }, {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                  presets: ['env'],
                  plugins: [require('babel-plugin-transform-object-assign')]
                }
            }
        }, {
            test: /\.css$/,
            loader: 'style-loader!css-loader!less-loader'
        }, {
            test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
            loader: 'file-loader'
        }, {
            test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
            loader: 'file-loader',
            query: {
                name: 'img/[name].[ext]?[hash]'
            }
        }]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue'
        }
    },
    devServer: {
        historyApiFallback: true,
        noInfo: true,
        proxy: {
            '/sports/api': {
                target: 'http://115.29.39.62:9080',
                // target: 'https://ezone.upesn.com',
                changeOrigin: true,
                secure: false
            }
        }
    },
}

if (process.env.NODE_ENV === 'production') {
    module.exports.devtool = '#source-map';
    module.exports.output.filename = '[name].[chunkhash].js';
    // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                drop_console: true
            }
        }),

        new webpack.LoaderOptionsPlugin({
            minimize: true,
            options: {
                vue: {
                    loaders: utils.cssLoaders(),
                    postcss: [
                        autoprefixer({
                            browsers: ['last 3 versions', "Android >= 2.3", "> 1%", "iOS >= 7"],
                        })
                    ]
                },
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: 'vendor.[chunkhash].js'
        }),
        new ExtractTextPlugin({
            filename: "app.[chunkhash].css",
            allChunks: true
        }),
        new CopyWebpackPlugin([
            {
                context: './src/assets/font', from: 'iconfont.*', to: 'font'
            }
        ]),
        // 自动注入 html
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.resolve(__dirname, './index.build.html'),
            inject: 'body'
        })
    ])
}
